package com.iqiju.guagua.photo.wx.mini.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class IDService {

    private static final String ID_URL = "https://id-gen.ihongyin.com/id";

    @Autowired
    private RestTemplate restTemplate;

    public long id(){
        try {
            JSONObject idObject = restTemplate.getForObject(ID_URL, JSONObject.class);
            String state = idObject.getString("state");
            if ("0".equals(state)){
                Long id = idObject.getLong("content");
                log.info("发号器返回结果：{}", id);
                return id;
            }else{
                log.error("发号器返回500");
                return IdWorker.getId();
            }
        } catch (Exception e) {
            log.error("远程获取id异常");
            log.error(e.getMessage(), e);
            return IdWorker.getId();
        }
    }

}
